package com.zxit.controller;

import com.zxit.entity.Dept;
import com.zxit.entity.Result;
import com.zxit.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/depts")
@RestController
public class DeptController {
    private  static final Logger log= LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;
   // @RequestMapping(value ="/depts",method = RequestMethod.GET)//指定请求方式
    @GetMapping
    public Result findByall() {
        log.info("查询所有部门");
        List<Dept> list = deptService.findByall();
        return Result.success(list);//传递数值给前端
    }

    /**
     * 删除部门 方式一
     */
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String str = request.getParameter("id");
//        int id = Integer.parseInt(str);
//        System.out.println("删除成功"+id);
//        return Result.success();
//    }
//    @DeleteMapping("/depts")
//    public Result delete(@RequestParam("id") Integer id){
//        deptService.deleteById(id);
//        System.out.println("删除成功"+id);
//        return Result.success();
//    }
    @DeleteMapping
    public Result delete(Integer id){
        deptService.delete(id);
        //System.out.println("删除成功"+id);
        log.info("删除成功"+id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept){
        //System.out.println("添加部门:" + dept);
        log.info("添加部门:" + dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") Integer id){
//        System.out.println("查询部门信息:" + id);
        log.info("查询部门信息:{}",id);
        Dept dept = deptService.getInfo(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
//        System.out.println("修改部门信息:" + dept);
        log.info("修改部门信息:{}",dept);
        int id=dept.getId();
        deptService.update(dept);
        return Result.success();
    }
}
